package 
{
	import flash.display.*;
	import flash.events.TimerEvent;
	import flash.filters.*;
	import flash.geom.*;
	import flash.utils.Timer;
	
	public class Objekt extends MovieClip
	{
		var xBounds:Array;
		var yBounds:Array;

		var position:Point;
		var direction:Point;
		
		var friction:Number;
		var mass:Number;
		var energy:Number;
		var speed:Number;
		var radius:Number;
		
		var step:Number;
		var objectTimer:Timer = new Timer(30,10);
		
		var color:uint;
		//public var in_objects:Array;
		
		public function Objekt(parent:DisplayObjectContainer, xBounds:Array, yBounds:Array)
		{
			this.xBounds = xBounds;
			this.yBounds = yBounds;
			
			this.x = 0;
			this.y = 0;
			
			friction = 1.0;
			direction = new Point(0,0);
			mass = 10.0;
			energy = 10.0;
			speed = 1.0;
			radius = mass/10;
			
			color = 0x000000;
			
			parent.addChild(this);
			drawObject(true);
		}
		
		public function setValues (ipos:Point,idir:Point,imass:Number,ifriction:Number,speed:Number):void
		{
			this.x = ipos.x;
			this.y = ipos.y;
			
			direction = idir;
			mass = imass;
			friction = ifriction;
			radius = mass/10;
			
			drawObject(true);
		}
		
		public function drawObject(doclear:Boolean):void
		{
			if (doclear) this.graphics.clear();
			
			this.graphics.lineStyle(1,this.color,0.1,true,"normal","round","round",1);
			
			this.graphics.beginFill(this.color,0.5);
			this.graphics.drawCircle(0, 0, radius);
			this.graphics.endFill();
			
			var object_glow:GlowFilter = new GlowFilter();
			object_glow.blurX = 10;
			object_glow.blurY = 10;
			object_glow.strength = 3.1;
			object_glow.color = this.color;
			object_glow.quality = BitmapFilterQuality.MEDIUM;
			this.filters = [object_glow];
			
		}
		
		public function updateObject(new_color:uint, new_energy:Number, new_mass:Number):void
		{
			color = new_color;
			energy = new_energy;
			mass = new_mass;
			radius = mass/10;
			
			drawObject(true);
		}
		
		public function moveObject():void
		{	
			//direction.x /= mass;
			//direction.y /= mass;
			var dl:Number = Math.sqrt(direction.x*direction.x + direction.y*direction.y);
			
			direction.x = 1.0 * direction.x * friction / (mass);
			direction.y = 1.0 * direction.y * friction / (mass);
			
			this.x += direction.x * speed;
			this.y += direction.y * speed;
			
			if ( y >= yBounds[1] || y <= yBounds[0] ) direction.x *= -1;
			if ( x >= xBounds[1] || x <= xBounds[0] ) direction.y *= -1;
			
			this.drawObject(true);
			this.graphics.lineStyle(2,0xFF0000,1,true,"normal","round","round",1);
			this.graphics.moveTo(0,0);
			this.graphics.lineTo(direction.x*100,direction.y*100);
		}
		
		public function grow(new_mass:Number):void {
			step = new_mass/10;
			objectTimer.reset();
			objectTimer.start();
			objectTimer.addEventListener(TimerEvent.TIMER, growStep);
		}
		
		public function growStep(evt:TimerEvent) {
			mass += step;
			updateObject(this.color,this.energy,mass);
		}
	}
}